//
//  HLVisitorView.swift
//  HL-sinaWeiBo
//
//  Created by 任翰林 on 16/5/11.
//  Copyright © 2016年 任翰林. All rights reserved.
//

import UIKit
import SnapKit
//import SnapKit

class HLVisitorView: UIView {

//    定义按钮点击事件的回调闭包
    var callBackClosure: (() -> ())?
    
    
    //  MARK: - 懒加载控件 -
    //旋转图片
    private lazy var cycleImageView: UIImageView = UIImageView(image: UIImage(named:"visitordiscover_feed_image_smallicon" ))
//    盖板
    private lazy var maskImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
//    中间的房子图片
    private lazy var iconImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
//    文本
    private lazy var messageLabel: UILabel = {
    
        let label = UILabel()
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(14)
        label.numberOfLines = 0
        label.textAlignment = .Center
        
        return label
    
    }()
    
//    注册按钮
    private lazy var registerButton: UIButton = {
    let button = UIButton()
        button.addTarget(self, action: "registerButtonClick", forControlEvents: .TouchUpInside)
        button.setTitle("注册", forState: .Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        return button
    
    }()
    
    //    登录按钮
    private lazy var loginButton: UIButton = {
        let button = UIButton()
        button.addTarget(self, action: "loginButtonClick", forControlEvents: .TouchUpInside)
        button.setTitle("登录", forState: .Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        return button
        
    }()
    
    
    //  MARK: - 重写init方法 -
    override init(frame: CGRect) {
    super.init(frame: frame)
        backgroundColor = UIColor(white: 237 / 255, alpha: 1)
        
//        添加控件
        setUpUI()
    
        
        
    }

//    使用xib或者sb的时候回调用，防止出错
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    //  MARK: - 添加控件 -
    func setUpUI() {
    
//  addSubview(cycleImageView)//旋转图片
        addSubview(cycleImageView)
        addSubview(maskImageView)
        addSubview(iconImageView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)

        

//        添加约束
        cycleImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        maskImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(cycleImageView)
        }
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(cycleImageView)
        }
        messageLabel.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(cycleImageView)
            make.top.equalTo(cycleImageView.snp_bottom)
            make.size.equalTo(CGSizeMake(230, 40))
        }
        registerButton.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(messageLabel.snp_bottom)
            make.leading.equalTo(messageLabel)
            make.size.equalTo(CGSizeMake(100, 35))
        }
        loginButton.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(messageLabel.snp_bottom)
            make.trailing.equalTo(messageLabel)
            make.size.equalTo(CGSizeMake(100, 35))
        }
        
    
    }
    
   //  MARK: - 按钮的点击事件 -
    
    func registerButtonClick() {
    
        print("我是小房子下面的注册按钮，我被点击了")
        callBackClosure?()
    
    }
    
    func loginButtonClick() {
        
        print("我是小房子下面的登录按钮，我被点击了")
        callBackClosure?()
        
    }
    
    
    //  MARK: - 添加开始动画 -
    
    func startAnimation() {
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = 2 * M_PI//目的地
        animation.duration = 20//旋转时常
        animation.repeatCount = MAXFLOAT//重复次数
        animation.removedOnCompletion = false
        cycleImageView.layer.addAnimation(animation, forKey: nil)
        
    }
    
    
   
    //  MARK: - 修改访客视图信息 -
    func setVisitorInfo(title: String? , imageName: String?) {
    
        if let t = title , imgName = imageName {
        
            messageLabel.text = t
            iconImageView.image = UIImage(named: imgName)
            cycleImageView.hidden = true
        
        }else{
        
            startAnimation()
            cycleImageView.hidden = false
        
        }
    
        
    }
    
    
    
    
//    系统的自动约束
//    
//    func TEST() {    
//        //        使用系统约束
//        //  使用系统的约束
//        //        cycleImageView.translatesAutoresizingMaskIntoConstraints = false
//        //        addConstraint(NSLayoutConstraint(item: cycleImageView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
//        //        addConstraint(NSLayoutConstraint(item: cycleImageView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: 0))
//        //
//        
//        cycleImageView.translatesAutoresizingMaskIntoConstraints = false
//        
//        addConstraint(NSLayoutConstraint(item: cycleImageView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
//        
//        addConstraint(NSLayoutConstraint(item: cycleImageView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: 0))
//        
//    }
    
    
}
